package com.qqq.commonmessage.service;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.google.gson.Gson;
import com.qqq.commoncore.utils.JsonUtil;
import com.qqq.commondomain.constants.MessageConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 短信服务
 */
@Component
@Slf4j
@RefreshScope
public class AliSmsService {
    
    
    /**
     * 客户端
     */
    @Autowired
    private Client client;
    
    
    /**
     * 默认模板代码，⼀般为注册短信的代码
     */
    @Value("${sms.aliyun.templateCode:}")
    private String templateCode;
    
    
    /**
     * 是否发送短信，不发送往往⽤于测试的时候
     */
    @Value("${sms.send-message:true}")
    private boolean sendMessage;
    
    
    /**
     * 签名配置
     */
    @Value("${sms.sing-name:}")
    private String singName;
    
    
    /**
     * 发送短信验证码
     *
     * @param phone ⼿机号
     * @param code 验证码
     * @return 是否成功
     */
    public boolean sendMobileCode(String phone, String code) {
        Map<String, String> params = new HashMap<>(1);
        params.put("code", code);
        return sendTempMessage(phone, templateCode, params);
    }
    
    
    /**
     * 发送模板消息
     *
     * @param phone ⼿机号
     * @param templateCode 模板code
     * @param params 参数
     * @return 是否成功
     */
    public boolean sendTempMessage(String phone, String templateCode,
                                   Map<String, String> params) {
        // 1 靠nacos控制是否发送短信
        if (!sendMessage) {
            log.error("短信发送通道关闭，发送失败......{}", phone);
            return false;
        }
        // 2 创建阿⾥云发送短信请求
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setPhoneNumbers(phone);
        sendSmsRequest.setSignName(singName);
        sendSmsRequest.setTemplateCode(templateCode);
        sendSmsRequest.setTemplateParam(JsonUtil.obj2string(params));
        // 3 根据返回结果判断短信是否发送成功
        try {
            SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            SendSmsResponseBody responseBody = sendSmsResponse.getBody();
            if (responseBody.getCode().equals(MessageConstants.SMS_MSG_OK)) {
                return true;
            }
                log.error("短信{} 发送失败，失败原因:{}.... ", new
                        Gson().toJson(sendSmsRequest), responseBody.getMessage());
                return false;
        } catch (Exception e) {
            log.error("短信{} 发送失败，失败原因:{}.... ", new Gson().toJson(sendSmsRequest), e.getMessage());
            return false;
        }
    }
}